﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;

namespace TD.TheGame
{
    public class ArrowSprite : Sprite
    {
        public int Damage
        {
            get;
            set;
        }

        protected Rectangle[] normalArrow;
        protected Rectangle[] futureArrow;
        protected Rectangle[] pastArrow;

        public ArrowSprite(Game game, Vector2 position, bool active, int maxLoopTime, SpriteEnumState state)
            : base(game, position, active, maxLoopTime, state)
        {
            normalArrow = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "Arrow\\" + "normalArrow.xml");
            futureArrow = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "Arrow\\" + "futureArrow.xml");
            pastArrow = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "Arrow\\" + "pastArrow.xml");
            current = pastArrow;
            Damage = 50;
        }

        public override void Update(GameTime gameTime)
        {
            if (Active)
            {
                //Check if State is changed, if changed: begin with the first sprite of the new array
                if (oldState != State)
                {
                    if (State == SpriteEnumState.Past)
                    {
                        current = pastArrow;
                    }
                    if (State == SpriteEnumState.Normal)
                    {
                        current = normalArrow;
                    }
                    if (State == SpriteEnumState.Future)
                    {
                        current = futureArrow;
                    }
                    //set the currentsprite different
                    currentRect = 0;
                }
                moveArrow();
                oldState = State;
                CollisionDetection();
                base.Update(gameTime);
            }
        }

        /// <summary>
        /// Collision Detection
        /// </summary>
        public void CollisionDetection()
        {
            if (position.X + BoundingRectangle.Width > Game.Window.ClientBounds.Width || position.X < 0)
            {
                Reset();
            }
            if (position.Y < 0 || position.Y + BoundingRectangle.Height > Game.Window.ClientBounds.Width)
            {
                Reset();
            }
        }

        /// <summary>
        /// Speed of arrow
        /// </summary>
        public void moveArrow()
        {
            position.X += 4;
        }

        protected override Texture2D getTexture()
        {
            //Init texture
            manager.LoadAsset("level\\Arrows");
            texture = manager.UseAsset("level\\Arrows");
            return texture;
        }

        /// <summary>
        /// Reset the sprite
        /// </summary>
        public override void Reset()
        {
            Active = false;
            position.X = 0;
            boundingRectangle.X = 0;
            boundingRectangle.Y = 0;
            position.Y = 0;
        }

        /// <summary>
        /// update Arrow sprite
        /// </summary>
        /// <param name="state"></param>
        /// <param name="damage"></param>
        public void Upgrade(SpriteEnumState state, int damage)
        {
            State = state;
            Damage = damage;
        }
    }
}
